const Service = require('egg').Service;
class HomeController extends Service {
  //查询用户
  async checkUser(obj) {
    try {
      const userList = await this.app.mysql.query('select * from frontuser where username =?', obj.username);
      return {
        code: 1,
        isExists: userList.length > 0
      }
    } catch (err) {
      return {
        code: 0,
        msg: err.message,
      };
    }
  }
  //添加用户
  async reigster(user) {
    try {
      await this.app.mysql.insert('frontuser', user)
      return {
        code: 1,
        msg: '注册成功'
      }
    } catch (e) {
      return {
        code: 0,
        msg: e.message
      }
    }
  }
  //
  async isLogin(obj) {
    try {
      const userList = await this.app.mysql.query('select * from user where username =? and password =?', [obj.username, obj.password])
      return {
        code: 1,
        isExists: userList.length > 0
      }
    } catch (err) {
      return {
        code: 0,
        msg: err.message
      };
    }
  }
  async getInfo(obj) {
    try {
      const user = await this.app.mysql.query('select * from user where username =?', obj.username)
      if (user.length > 0) {
        return {
          code: 200,
          data: {
            roles:user[0].roles.split(','),
            name:obj.username,
            avatar:user[0].avatar,
            introduction:"test"
          }
        }
      } else {
        return {
          code: 0,
          message:"查找失败"
        }
      }
    } catch (err) {
      return {
        code: 0,
        msg: err.message
      };
    }
  }
  async getList(roles){
    const {ctx} =this
    try{
      const user = await this.app.mysql.query('select * from user where roles=?',roles)
      return ctx.body = {
        code:200,
        data:user
      }
    }catch(e){
      return ctx.body={
        code:0,
        message:e.message
      } 
    }
  }
}

module.exports = HomeController;
